//
//  Address.swift
//  DiDiRecycle
//
//  Created by abb on 16/7/18.
//  Copyright © 2016年 abb. All rights reserved.
//

import Foundation

class Address : NSObject, NSCoding{
    
    /// 详细地址
    var addressDetailed : String?
    /// SDK地址
    var addressSdk : String?
    /// 联系人
    var contactName : String?
    
    /// 联系方式
    var contactPhoneNumber : String?
    
    /// 地址ID
    var id : Int?
    /// 纬度
    var latitude : CGFloat?
    /// 经度
    var longitude : CGFloat?
    /// 用户ID
    var userId : Int = 0
    /// 是否是默认
    var isDefault : Int = 4
    
    // MARK: - 公共方法
//    /**
//     获取所有地址并转换为模型
//     
//     - parameter userID:   用户id
//     - parameter finished: 回调闭包
//     */
//    class func loadAddress(userId userID:Int,finished:(models:[Address]?,error:String?)->()) {
//        AddressDAO.getAddress(userID: userID) { (array, error) in
//            // 错误判断
//            if array == nil {
//                finished(models: nil, error: error)
//                return
//            }
//            if error != nil {
//                finished(models: nil, error: error)
//                return
//            }
//            // 遍历数组,将字典转换为模型
//            var models = [Address]()
//            for dict in array! {
//                models.append(Address(fromDictionary: dict))
//            }
//            // 回调闭包
//            finished(models: models, error: nil)
//        }
//    }
//    
//    class func loadUserDefaultAddress(userID userID:Int,finished:(model:Address?,error:String?)->()) {
//        AddressDAO.getUserDefaultAddress(userId: userID) { (dict, error) in
//            // 错误判断
//            if dict == nil {
//                finished(model: nil, error: error)
//                return
//            }
//            if error != nil {
//                finished(model: nil, error: error)
//                return
//            }
//            // 将字典转换为模型
//            let model = Address(fromDictionary: dict!)
//            // 回调闭包
//            finished(model: model, error: nil)
//        }
//    }
    
    // MARK: - 内部方法
    
    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:AnyObject]){
        addressDetailed = dictionary["address_detailed"] as? String
        addressSdk = dictionary["address_sdk"] as? String
        contactName = dictionary["contact_name"] as? String
        contactPhoneNumber = dictionary["contact_phone_number"] as? String
        id = dictionary["id"] as? Int
        latitude = dictionary["latitude"] as? CGFloat
        longitude = dictionary["longitude"] as? CGFloat
        userId = dictionary["user_id"] as! Int
        isDefault = dictionary["is_default"] as! Int
    }
    
    
    init(fromDictionaryForPost dictionary: NSDictionary){
        addressDetailed = dictionary["address_detailed"] as? String
        addressSdk = dictionary["address_sdk"] as? String
        contactName = dictionary["contact_name"] as? String
        contactPhoneNumber = dictionary["contact_phone_number"] as? String
        latitude = dictionary["latitude"] as? CGFloat
        longitude = dictionary["longitude"] as? CGFloat
    }
    
    
    init(userId:Int) {
        super.init()
        self.userId = userId
    }
    
    /**
     * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:AnyObject]
    {
        var dictionary = [String:AnyObject]()
        if addressDetailed != nil{
            dictionary["address_detailed"] = addressDetailed
        }
        if addressSdk != nil{
            dictionary["address_sdk"] = addressSdk
        }
        if contactName != nil{
            dictionary["contact_name"] = contactName
        }
        if contactPhoneNumber != nil{
            dictionary["contact_phone_number"] = contactPhoneNumber
        }
        if id != nil{
            dictionary["id"] = id
        }
        if latitude != nil{
            dictionary["latitude"] = latitude
        }
        if longitude != nil{
            dictionary["longitude"] = longitude
        }
        if userId != 0{
            dictionary["user_id"] = userId
        }
        if isDefault != 4{
            dictionary["is_default"] = isDefault
        }
        return dictionary
    }
    
    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    @objc required init(coder aDecoder: NSCoder)
    {
        addressDetailed = aDecoder.decodeObjectForKey("address_detailed") as? String
        addressSdk = aDecoder.decodeObjectForKey("address_sdk") as? String
        contactName = aDecoder.decodeObjectForKey("contact_name") as? String
        contactPhoneNumber = aDecoder.decodeObjectForKey("contact_phone_number") as? String
        id = aDecoder.decodeObjectForKey("id") as? Int
        latitude = aDecoder.decodeObjectForKey("latitude") as? CGFloat
        longitude = aDecoder.decodeObjectForKey("longitude") as? CGFloat
        userId = aDecoder.decodeObjectForKey("user_id") as! Int
        isDefault = aDecoder.decodeObjectForKey("is_default") as! Int
        
    }
    
    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    @objc func encodeWithCoder(aCoder: NSCoder)
    {
        if addressDetailed != nil{
            aCoder.encodeObject(addressDetailed, forKey: "address_detailed")
        }
        if addressSdk != nil{
            aCoder.encodeObject(addressSdk, forKey: "address_sdk")
        }
        if contactName != nil{
            aCoder.encodeObject(contactName, forKey: "contact_name")
        }
        if contactPhoneNumber != nil{
            aCoder.encodeObject(contactPhoneNumber, forKey: "contact_phone_number")
        }
        if id != nil{
            aCoder.encodeObject(id, forKey: "id")
        }
        if latitude != nil{
            aCoder.encodeObject(latitude, forKey: "latitude")
        }
        if longitude != nil{
            aCoder.encodeObject(longitude, forKey: "longitude")
        }
        if userId != 0{
            aCoder.encodeObject(userId, forKey: "user_id")
        }
        if isDefault != 4{
            aCoder.encodeObject(isDefault, forKey: "is_default")
        }
        
    }
    
}